package org.example.Conrtoller;

import redis.clients.jedis.Jedis;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class DailyUniqueIdGenerator {
    //    private static final String REDIS_KEY_PATTERN = "id_counter_%s"; // Redis键模式
//    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
//    private final Jedis jedis;
//    private final String currentDateKey;
//
//    public DailyUniqueIdGenerator(Jedis jedis) {
//        this.jedis = jedis;
//        this.currentDateKey = String.format(REDIS_KEY_PATTERN, LocalDate.now().format(DATE_FORMATTER));
//    }
//
//    public String getNextId() {
//        // 使用Redis的INCR命令自增ID
//        long increment = jedis.incr(currentDateKey);
//
//        // 格式化ID为"yyyyMMddXXXX"形式，其中XXXX是自增的ID
//        String id = String.format("%s%04d", LocalDate.now().format(DATE_FORMATTER), increment);
//
//        // 设置Redis键的过期时间，确保每天重置
//        jedis.expire(currentDateKey, 86400);
//
//        return id;
//    }
//
//    public static void main(String[] args) {
//        // 创建Jedis实例连接到Redis
//        try (Jedis jedis = new Jedis("localhost", 6379)) {
//            DailyUniqueIdGenerator generator = new DailyUniqueIdGenerator(jedis);
//
//            // 模拟生成ID
//            for (int i = 0; i < 10; i++) {
//                String uniqueId = generator.getNextId();
//                System.out.println(uniqueId);
//            }
//
//            // 假设这是第二天，生成新的ID
//            currentDateKey = String.format(REDIS_KEY_PATTERN, LocalDate.now().format(DATE_FORMATTER));
//            for (int i = 0; i < 5; i++) {
//                String uniqueId = generator.getNextId();
//                System.out.println(uniqueId);
//            }
//        }
//    }
    private static final String REDIS_KEY_PATTERN = "id_counter_%s"; // Redis键模式
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
    private final Jedis jedis;
    private final String currentDateKey;

    public DailyUniqueIdGenerator(Jedis jedis) {
        this.jedis = jedis;
        this.currentDateKey = String.format(REDIS_KEY_PATTERN, LocalDate.now().format(DATE_FORMATTER));
    }

    public String getNextId() {
        // 使用Redis的INCR命令自增ID
        long increment = jedis.incr(currentDateKey);

        // 格式化ID为"yyyyMMddXXXX"形式，其中XXXX是自增的ID
        String id = String.format("%s%04d", LocalDate.now().format(DATE_FORMATTER), increment);

        // 设置Redis键的过期时间，确保每天重置
        jedis.expire(currentDateKey, 86400);
//        jedis.expire(currentDateKey, 10);

        return id;
    }

    public static void main(String[] args) {
        // 创建Jedis实例连接到Redis
        try (Jedis jedis = new Jedis("localhost", 6379)) {
            DailyUniqueIdGenerator generator;
            //加同步锁，防止id重复
            synchronized (jedis) {
                generator = new DailyUniqueIdGenerator(jedis);

                // 模拟生成ID
                for (int i = 0; i < 10; i++) {
                    String uniqueId = generator.getNextId();
                    System.out.println(uniqueId);
                }
                Thread.sleep(15000);
                System.out.println("======================================");
                //模拟第二天生成的id,序号从1开始
                for (int i = 0; i < 10; i++) {
                    String uniqueId = generator.getNextId();
                    System.out.println(uniqueId);
                }
            }
            // 假设这是第二天，生成新的ID
            String currentDateKey = String.format(REDIS_KEY_PATTERN, LocalDate.now().format(DATE_FORMATTER));
            for (int i = 0; i < 5; i++) {
                String uniqueId = generator.getNextId();
                System.out.println(uniqueId);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}